﻿using ErgoBreak.Data;
using System.Timers;

namespace ErgoBreak.Core
{
    public class CoreBuilder
    {
        #region Fields
        private Settings settings;
        private ObserverFactory obsFactory;
        private Controller controller;
        private ConfigurationManager configManager;
        #endregion

        #region Events

        #endregion

        #region Constructors
        public CoreBuilder(Settings appSettings)
        {
            this.settings = appSettings;
            this.obsFactory = new ObserverFactory(appSettings);
        }
        #endregion

        #region Properties
        /// <summary>
        /// Gets the controller instance built.
        /// </summary>
        public IController MainController
        {
            get
            {
                return this.controller;
            }
        }

        /// <summary>
        /// Gets the ConfigurationManager instance built.
        /// </summary>
        public ConfigurationManager ConfigManager
        {
            get
            {
                return this.configManager;
            }
        }
        #endregion

        #region Public Methods
        public void Build()
        {
            // start building the 'Core' layer
            BreakManager manager = BuildManager();
            this.controller = BuildController(manager);
            this.configManager = BuildConfigurationManager(controller);
        }
        #endregion

        #region Non-Public Methods
        private BreakManager BuildManager()
        {
            return new BreakManager(new Timer());
        }

        private Controller BuildController(BreakManager manager)
        {
            BreakActionFactory factory = new BreakActionFactory(this.settings);
            ObserverBase observer = this.obsFactory.CreateObserver();
            return new Controller(manager, observer, factory);
        }

        private ConfigurationManager BuildConfigurationManager(Controller controller)
        {
            return new ConfigurationManager(this.settings, controller, this.obsFactory);
        }
        #endregion
    }
}
